#!/bin/bash
SCRIPT_BASE_DIR=$( dirname "${BASH_SOURCE[0]}" )
source "${SCRIPT_BASE_DIR}/common/eb_utils.sh"
source "${SCRIPT_BASE_DIR}/common/fs_utils.sh"

set -e
targetOS="eblinux"
targetPlatform="qemu-x86"

function usage() {
    echo "$( basename $0 ) [-a <top_dir> | -m <app_dir1> -m <app_dir2> ...]"
    echo "      把编译好的eb app程序封装进Container"
    echo "      参数说明:"
    echo "      -a <top_dir> 对<top_dir>下所有eb app进行操作"
    echo "      -m <app_dir> 仅对指定的<app_dir>进行操作"
    exit 1
}

function create_container() {
    local d=$1
    dir_exists_or_die "$d"
    dir_exists_or_die "$d/generated"

    echo ara-cli CreateAppContainer --app-dir "${d}" --config-dir "${d}/generated" --container-dir "${d}/container"
    ara-cli CreateAppContainer --app-dir "${d}" --config-dir "${d}/generated" --container-dir "${d}/container"
    sleep 2
    echo "***********************************************************"
}

apps=()
while getopts ":a:m:" arg
do
    case $arg in
        a)
            apps=($( all_apps "${OPTARG}" ))
            ;;
        m)
            apps[${#apps[@]}]="${OPTARG}"
            ;;
        *)
            usage
            ;;
    esac
done

if [ 0 -eq ${#apps[@]} ]
then
    usage
fi

# echo "${apps[@]}"
# exit 

for app in "${apps[@]}"
do
    create_container "${app}"
done
